import java.util.Comparator;
import java.util.PriorityQueue;

public class Solution1046 {
    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> queue=new PriorityQueue<>(Comparator.reverseOrder());
        for (int stone : stones) {
            queue.offer(stone);
        }
        while (queue.size()>1){
            int a=queue.poll();
            int b=queue.poll();
            if (a>b){
                queue.offer(a-b);
            }
        }
        return queue.size()==0?0:queue.peek();
    }

    public static void main(String[] args) {
        System.out.println(new Solution1046().lastStoneWeight(new int[]{2,7,4,1,8,1}));
    }
}
